package cc.vv.btpartyjob.partyservice.activity

import android.os.Message
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.util.LKTimeUtil
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.baselibrary.view.util.DateChoseDialog
import cc.vv.btpartyjob.jnimodule.binder.BaseBinder
import cc.vv.btpartyjob.jnimodule.server.ResourceChooseServer
import cc.vv.btpartyjob.partyservice.R
import cc.vv.btpartyjob.partyservice.delegate.SubmitMicroDonationActivityDelegate
import cc.vv.btpartyjob.partyservice.module.resp.SubMicDonationResponseObj
import cc.vv.btpartyjob.partyservice.module.resp.SubTinyWishResponseObj
import cc.vv.btpartyjob.partyservice.server.DataPartyServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 发布 微捐赠
 * Created by Chen.qi on 2018/11/7.
 */
class SubmitMicroDonationActivity : BaseActivityMVP<SubmitMicroDonationActivityDelegate>() {
    /**
     * 开始时间
     */
    private var startTime: Long = 0

    /**
     * 结束时间
     */
    private var endTime: Long = 0

    override fun initData() {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
        viewDelegate?.setTopBarTitle("发布微捐赠")
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //开始时间
            ResourceChooseServer.chooseData(this, handler, 20, DateChoseDialog.DATE_TYPE_3)
        }, R.id.ll_asmD_startTime)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //结束时间
            ResourceChooseServer.chooseData(this, handler, 21, DateChoseDialog.DATE_TYPE_3)
        }, R.id.ll_asmD_endTime)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //提交
            val name = viewDelegate?.getName()
            val tel = viewDelegate?.getMobile()
            val title = viewDelegate?.getAddress()
            val content = viewDelegate?.getContent()

            if (TextUtils.isEmpty(name) || TextUtils.isEmpty(tel) || TextUtils.isEmpty(title) || TextUtils.isEmpty(content)) {
                LKToastUtil.showToastShort("请将数据填写完整")
                return@OnClickListener
            }

            if (startTime == 0L || endTime == 0L) {
                LKToastUtil.showToastShort("请选择时间")
                return@OnClickListener
            }

            if (startTime >= endTime) {
                LKToastUtil.showToastShort("结束时间必须大于开始时间")
                return@OnClickListener
            }
            DataPartyServer.subMicDonationHttp(name, tel, title, content, startTime, endTime, true)
        }, R.id.rl_asmD_submit)
    }

    override fun getDelegateClass(): Class<SubmitMicroDonationActivityDelegate> {
        return SubmitMicroDonationActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }

    override fun onHandleMsg(msg: Message?) {
        super.onHandleMsg(msg)
        when (msg?.what) {
            20 -> {
                //开始时间
                val startString: String = msg.obj as String
                startTime = LKTimeUtil.getInstance().getLongTime(startString, "yyyy-MM-dd")
                viewDelegate?.setStartTime(startString)
            }
            21 -> {
                //结束时间
                val endString: String = msg.obj as String
                endTime = LKTimeUtil.getInstance().getLongTime(endString, "yyyy-MM-dd")
                viewDelegate?.setEndTime(endString)
            }
        }


    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is SubMicDonationResponseObj -> {
                LKToastUtil.showToastShort("发布成功,等待审核")
                finish()
            }
        }
    }
}